import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:high_help_apps/common/apis/v1/user.dart';
import 'package:high_help_apps/common/entities/user/user_register_response_entity.dart';
import 'package:high_help_apps/common/routes/app_pages.dart';
import 'package:high_help_apps/common/utils/toast.dart';
import 'package:high_help_apps/common/values/colors.dart';
import 'package:image_picker/image_picker.dart';

import '../sign_up_step_logic.dart';
import '../sign_up_step_state.dart';
import 'register_user_detail_state.dart';

class RegisterUserDetailLogic extends GetxController {
  final state = RegisterUserDetailState();
  final SignUpStepState signUpStepState = Get
      .find<SignUpStepLogic>()
      .state;

  void finishRegister() async {
    if (
    state.email == '' ||
        state.nick_name == '' ||
        state.sex == null ||
        state.phone == ''
    ) {
      toastInfo(msg: '啊哦, 社区信息还没有填写完整呢 😓',
          backgroundColor: AppColors.lightTextColor);
      return;
    }

    signUpStepState.userRegisterRequestEntity.email = state.email;
    signUpStepState.userRegisterRequestEntity.nickName = state.nick_name;
    signUpStepState.userRegisterRequestEntity.sex = state.sex;
    signUpStepState.userRegisterRequestEntity.phone = state.phone;

    // uploadAvatar();

    UserRegisterResponseEntity responseEntity = await UserAPI.register(
        params: signUpStepState.userRegisterRequestEntity);

    Get.offAllNamed(AppRoutes.EMAIL_AUTH_PAGE, arguments: {
      'email': state.email,
      'uid': responseEntity.detail!.uid
    });
  }

  void finishInputRealName(String value) {
    state.nick_name = value;
    update();
    Get.back();
  }

  void finishInputEmail(String value) {
    state.email = value;
    update();
    Get.back();
  }

  void finishChooseSex(int index) {
    state.sex = index;
    update();
  }

  void finishInputPhone(String value) {
    state.phone = value;
    update();
    Get.back();
  }

}
